package com.card.utils;

import lombok.Getter;
import lombok.Setter;

/**
 * 统一返回类
 */
@Getter
@Setter
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    /**
     * 无参成功
     * @return
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMsg("成功");
        return result;
    }

    /**
     * 有参成功
     * @param data Object类型返回参数
     * @return
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(200);
        result.setMsg("成功");
        return result;
    }

    /**
     * 有参成功
     * @param code
     * @param data Object类型返回参数
     * @return
     */
    public static <T> Result<T> success(int code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 失败
     * @return
     */
    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.setCode(400);
        return result;
    }

    /**
     * 失败
     * @param msg 失败信息
     * @return
     */
    public static <T> Result<T> error(String msg) {
        Result<T> result = new Result<>();
        result.setCode(400);
        result.setMsg(msg);
        return result;
    }

    /**
     * 失败
     * @param code 状态码
     * @param msg  失败信息
     * @return
     */
    public static <T> Result<T> error(int code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}

